﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace DentScan2.WebApp
{
    public partial class Areas : Classes.BaseForm
    {

        public Guid? CurrentAreaId
        {
            get
            {
                return ViewState["CurrentAreaId"] == null ? null : ViewState["CurrentAreaId"] as Guid?;
            }
            set
            {
                ViewState["CurrentAreaId"] = value;
            }
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            this.CheckLogged();
            if (!IsPostBack)
            {
                Search();
                InitDDL();
            }

            if (this.Level == 2)
            {
                Edit(this.AreaId.ToString());
            }

        }

        private void InitDDL()
        {
            for (int i = 0; i <= 100; i++)
            {
                ddlAlum.Items.Add(new ListItem(i.ToString(), i.ToString()));
                ddlPNP.Items.Add(new ListItem(i.ToString(), i.ToString()));
                ddlPolish.Items.Add(new ListItem(i.ToString(), i.ToString()));
                ddlVAT.Items.Add(new ListItem(i.ToString(), i.ToString()));
            }

            for (int i = 0; i <= 100; i++)
                ddlAWToHour.Items.Add(new ListItem(i.ToString(), i.ToString()));

            for (int i = 1; i <= 200; i++)
                ddlHourPrice.Items.Add(new ListItem(i.ToString(), i.ToString()));

            for (int i = 1; i <= 200; i++)
                ddlDissasemblyHourPrice.Items.Add(new ListItem(i.ToString(), i.ToString()));

            for (int i = 1; i <= 200; i++)
                ddlPreparationHourPrice.Items.Add(new ListItem(i.ToString(), i.ToString()));

            cblMethods.DataSource = dl.AWMethods.Where(m => !m.IsDel);
            cblMethods.DataBind();
            cblMethods.Items.Insert(0, new ListItem("Почасово изчисление", "-1"));

        }
        private void Search()
        {
            rAreas.DataSource = dl.Areas.Where(ar => !ar.IsDel).OrderBy(ar => ar.AreaName);
            rAreas.DataBind();
        }

        private void Save()
        {
            Page.Validate("vgArea");
            if (!Page.IsValid)
                return;



            if (this.CurrentAreaId == null)
            {
                DB.Area ar = new DB.Area()
                {
                    AreaId = Guid.NewGuid(),
                    AreaName = tbAreaName.Text,
                    AreaShortName = tbAreaShortName.Text,
                    HourPrice = int.Parse(ddlHourPrice.SelectedValue),
                    AWCurrency = tbAWCurrency.Text,
                    AWToHour = int.Parse(ddlAWToHour.SelectedValue),
                    VAT = int.Parse(ddlVAT.SelectedValue),
                    Alum = int.Parse(ddlAlum.SelectedValue),
                    DisassemblyHourPrice = decimal.Parse(ddlDissasemblyHourPrice.SelectedValue),
                    PreparationHourPrice = decimal.Parse(ddlPreparationHourPrice.SelectedValue),
                    PNPPercent = int.Parse(ddlPNP.SelectedValue),
                    PolishPercent = int.Parse(ddlPolish.SelectedValue),
                    LastEditDate = DateTime.UtcNow,
                    OwnData = tbOwnData.Content,
                    CanCaluclateHourly = cbCanCaluclateHourly.Checked
                };
                dl.Areas.InsertOnSubmit(ar);
                dl.SubmitChanges();

            }
            else
            {
                DB.Area ar = dl.Areas.Where(item => item.AreaId == this.CurrentAreaId.Value).First();
                ar.AreaName = tbAreaName.Text;
                ar.AreaShortName = tbAreaShortName.Text;
                ar.HourPrice = int.Parse(ddlHourPrice.SelectedValue);
                ar.AWCurrency = tbAWCurrency.Text;
                ar.AWToHour = int.Parse(ddlAWToHour.SelectedValue);
                ar.VAT = int.Parse(ddlVAT.SelectedValue);
                ar.Alum = int.Parse(ddlAlum.SelectedValue);
                ar.DisassemblyHourPrice = int.Parse(ddlDissasemblyHourPrice.SelectedValue);
                ar.PreparationHourPrice = int.Parse(ddlPreparationHourPrice.SelectedValue);
                ar.PNPPercent = int.Parse(ddlPNP.SelectedValue);
                ar.PolishPercent = int.Parse(ddlPolish.SelectedValue);
                ar.LastEditDate = DateTime.UtcNow;
                ar.OwnData = tbOwnData.Content;
                ar.CanCaluclateHourly = cbCanCaluclateHourly.Checked;
                dl.SubmitChanges();

            }
            if (this.Level == 2)
                return;
            Search();
            mvAreas.SetActiveView(vAreas);
        }



        private void Cancel()
        {
            if (this.Level == 2)
                return;
            mvAreas.SetActiveView(vAreas);
        }

        private void Edit(string areaId)
        {
            this.CurrentAreaId = Guid.Parse(areaId);
            DB.Area ar = dl.Areas.Where(item => item.AreaId == this.CurrentAreaId.Value).First();


            tbAreaName.Text = ar.AreaName;
            tbAreaShortName.Text = ar.AreaShortName;
            ddlHourPrice.SelectedValue = ((int)ar.HourPrice).ToString();
            tbAWCurrency.Text = ar.AWCurrency;
            ddlAWToHour.SelectedValue = ar.AWToHour.ToString();
            ddlVAT.SelectedValue = ar.VAT.ToString();
            ddlAlum.SelectedValue = ar.Alum.ToString();
            ddlDissasemblyHourPrice.SelectedValue = ((int)ar.DisassemblyHourPrice).ToString();
            ddlPreparationHourPrice.SelectedValue = ((int)ar.PreparationHourPrice).ToString();
            ddlPNP.SelectedValue = ar.PNPPercent.ToString();
            ddlPolish.SelectedValue = ar.PolishPercent.ToString();
            tbOwnData.Content = ar.OwnData;
            cbCanCaluclateHourly.Checked = ar.CanCaluclateHourly;
            if (this.Level == 2)
                DisableForOperator();
            mvAreas.SetActiveView(vEdit);
        }

        private void DisableForOperator()
        {
            tbAreaName.Enabled = false;
            tbAreaShortName.Enabled = false;
            tbAWCurrency.Enabled = false;
            ddlAWToHour.Enabled = false;
            ddlVAT.Enabled = false;
            tbOwnData.Visible = false;
            cbCanCaluclateHourly.Enabled = false;
        }

        private void New()
        {
            this.CurrentAreaId = null;
            tbAreaName.Text = "";
            tbAreaShortName.Text = "";
            ddlHourPrice.SelectedValue = "1";
            tbAWCurrency.Text = "";
            ddlAWToHour.SelectedValue = "1";
            ddlVAT.SelectedValue = "1";
            ddlAlum.SelectedValue = "1";
            ddlDissasemblyHourPrice.SelectedValue = "1";
            ddlPreparationHourPrice.SelectedValue = "1";
            ddlPNP.SelectedValue = "1";
            ddlPolish.SelectedValue = "1";
            tbOwnData.Content = "";
            mvAreas.SetActiveView(vEdit);
        }

        protected void rAreas_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "DEACTIVATE":
                    DB.Area ar = dl.Areas.Where(item => item.AreaId == Guid.Parse(e.CommandArgument.ToString())).First();
                    ar.IsDel = !ar.IsDel;
                    dl.SubmitChanges();
                    Search();
                    break;
                case "EDIT": this.Edit(e.CommandArgument.ToString()); break;
                case "METHODS": this.CurrentAreaId = Guid.Parse(e.CommandArgument.ToString()); this.ShowMethods(); break;

            }
        }

        protected void bNew_Click(object sender, EventArgs e)
        {
            this.New();
        }

        protected void bSave_Click(object sender, EventArgs e)
        {
            this.Save();
        }

        protected void bCancel_Click(object sender, EventArgs e)
        {
            this.Cancel();
        }

        private void ShowMethods()
        {
            foreach (ListItem item in cblMethods.Items)
                item.Selected = false;

            DB.Area area = dl.Areas.First(a => a.AreaId == this.AreaId.Value);
            bool hourly = area.CanCaluclateHourly;


            foreach (var method in dl.Area2AWMethods.Where(m => m.AreaId == this.AreaId.Value && !m.IsDel))
                foreach (ListItem item in cblMethods.Items)
                    if (item.Value == method.AWMethodId.ToString() || (item.Value == "-1" && hourly))
                        item.Selected = true;
            mvAreas.SetActiveView(vMethods);
        }

        private void SaveMethods()
        {
            foreach (var method in dl.Area2AWMethods.Where(m => m.AreaId == this.AreaId.Value && !m.IsDel))
                method.IsDel = true;
            DB.Area area = dl.Areas.First(a => a.AreaId == this.AreaId.Value);
            area.CanCaluclateHourly = false;
                        
            foreach (ListItem item in cblMethods.Items)
                if (item.Selected)
                {
                    if (item.Value != "-1")
                        dl.Area2AWMethods.InsertOnSubmit(new DB.Area2AWMethod()
                        {
                            Area2AWMethodId = Guid.NewGuid(),
                            IsDel = false,
                            LastEditDate = DateTime.Now,
                            AWMethodId = int.Parse(item.Value),
                            AreaId = this.AreaId.Value
                        });
                    else
                    {
                        area.CanCaluclateHourly = true;                        
                    }
                };
            dl.SubmitChanges();
            mvAreas.SetActiveView(vAreas);
        }

        private void CancelMethods()
        {
            mvAreas.SetActiveView(vAreas);
        }

        protected void bSaveMethods_Click(object sender, EventArgs e)
        {
            SaveMethods();
        }

        protected void bCancelMethods_Click(object sender, EventArgs e)
        {
            CancelMethods();
        }
    }
}